﻿using FangDuShi_Admin.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AspNet.Mvc.Grid.Pagination;

namespace FangDuShi_Admin.Controllers
{
      [OutputCache(Duration = 0, VaryByParam = "none")]
    public class IPWhiteListController : AdminBaseController
    {
        BLL.IPWhiteList ipBll = new BLL.IPWhiteList();
        BLL.Common comBll = new BLL.Common();
        int Pagesize = 10;
        //
        // GET: /IPWhiteList/
        public ActionResult Index(string Page, string startip, string endip, string note)
        {
            string strWhere = "1=1";
           
            if (!string.IsNullOrEmpty(note))
            {
                strWhere += " and FNote like '%" + note + "%'";
                ViewData["note"] = note;
            }

            int pageout;
            if (!int.TryParse(Page, out pageout))
            {
                pageout = 1;
            }

            List<Model.IPWhiteList> ipList = ipBll.GetModelList(strWhere);
            if (!string.IsNullOrEmpty(startip))
            {
               
               // strWhere += " and  FStartIP=[dbo].[fn_IP2Int](" + startip + ")";
          
                ipList = ipList.Where(h => validateIP(startip, h.FStartIPStr)).ToList();
                ViewData["startip"] = startip;
            }
            if (!string.IsNullOrEmpty(endip))
            {
                //strWhere += " and  FEndIP=[dbo].[fn_IP2Int](" + endip + ")";

                ipList = ipList.Where(h => validateIP( h.FEndIPStr,endip)).ToList();
                ViewData["endip"] = endip;
            }

            IPagination<Model.IPWhiteList> ipListout = ipList.AsPagination(pageout, Pagesize);


            PagerData pagerInfo = new PagerData(pageout, ipListout.TotalItems, Pagesize);
            ViewData["pageout"] = pageout;
            ViewData["Page"] = pagerInfo;
            return View(ipListout);
        }

        public ulong getipint(string startip)
        {
             System.Net.IPAddress x = System.Net.IPAddress.Parse(startip);
            

                ulong xl = BitConverter.ToUInt32(x.GetAddressBytes(), 0);
                return xl;
        }

        private bool validateIP(string startIP, string endIP)
        {
            // 分离出ip中的四个数字位
            string[] startIPArray = startIP.Split('.');
            string[] endIPArray = endIP.Split('.');

            // 取得各个数字
            long[] startIPNum = new long[4];
            long[] endIPNum = new long[4];
            for (int i = 0; i < 4; i++)
            {
                startIPNum[i] = long.Parse(startIPArray[i].Trim());
                endIPNum[i] = long.Parse(endIPArray[i].Trim());
            }

            // 各个数字乘以相应的数量级
            long startIPNumTotal = startIPNum[0] * 256 * 256 * 256 + startIPNum[1] * 256 * 256 + startIPNum[2] * 256 + startIPNum[3];
            long endIPNumTotal = endIPNum[0] * 256 * 256 * 256 + endIPNum[1] * 256 * 256 + endIPNum[2] * 256 + endIPNum[3];

            if (startIPNumTotal > endIPNumTotal)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public ActionResult IPadd(string actionOP = BaseStruct.ActionOperationStruct.ADD_NEXT)
        {

            return IPDoEdit("IPadd", "0", actionOP);
        }

        public ActionResult IPedit(string FID, string actionOP = BaseStruct.ActionOperationStruct.EDIT)
        {

            return IPDoEdit("IPadd", FID, actionOP);
        }

        private ActionResult IPDoEdit(string viewname, string FID, string actionOP)
        {
            Model.IPWhiteList mdl = new Model.IPWhiteList();
            switch (actionOP)
            {
                case BaseStruct.ActionOperationStruct.ADD_NEXT:
                    {

                        break;
                    }
                case BaseStruct.ActionOperationStruct.EDIT:
                    {

                        mdl = ipBll.GetModel(decimal.Parse(FID));
                        break;
                    }
            }

            ViewData["hdMethod"] = actionOP.ToString();
            return View(viewname, mdl);
        }
        public string DelList(string ids)
        {

            List<string> listid = ids.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
            string returnstr = ipBll.DelIPList(listid);
            if (returnstr == "ok")
            {
                return "ok";
            }
            else
            {
                return "删除失败错误原因:" + returnstr;
            }
        }
        public string Save()
        {

            Model.IPWhiteList mdl = new Model.IPWhiteList();

            string startip = Fetch.Post("startip").Trim();
            string endip = Fetch.Post("endip").Trim();
            string note = Fetch.Post("fck1").Trim();

            mdl.FNote = note;

            switch (Fetch.Post("hdMethod"))
            {
                case BaseStruct.ActionOperationStruct.ADD_NEXT:
                    {

                        try
                        {
                            decimal outid = ipBll.AddNew(mdl, startip, endip);

                            if (outid != 0)
                            {
                                return "操作成功";
                            }
                        }
                        catch (System.Exception ex)
                        {
                            return "错误:" + ex.Message;
                        }
                        break;
                    }
                case BaseStruct.ActionOperationStruct.EDIT:
                    {

                        try
                        {
                            string FID = Fetch.Post("hFID");
                            mdl.FID =decimal.Parse( FID.ToString());
                            if (ipBll.UpdateNew(mdl, startip, endip))
                            {
                                return "操作成功";
                            }
                            else
                            {

                                return "错误:保存数据失败";
                            }
                        }
                        catch (System.Exception ex)
                        {
                            return "错误:" + ex.Message;
                                     
                        }
                        break;
                    }
            }

            return "错误:保存数据失败";
        }
    }
}